package com.mumuso.mumusoitem.controller;

import com.mumuso.mumusoitem.pojo.dArcItemPre;
import com.mumuso.mumusoitem.pojo.dGoodsCollectionItemRole;
import com.mumuso.mumusoitem.service.dArcItemPreService;
import com.mumuso.mumusoitem.service.dGoodsCollectionRoleService;
import com.mumuso.mumusoitem.utils.AjaxResult;
import com.mumuso.mumusoitem.utils.HttpStatus;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.mumuso.mumusoitem.utils.ParseUrl.getUrl;

@RestController
@RequestMapping("/item")
public class itemRecordsController {

    @Autowired
    private dArcItemPreService dArcItemPreService;
    @Autowired
    private dGoodsCollectionRoleService dGoodsCollectionRoleService;


    /**
     * 获取所有的商品档案信息
     *
     * @return
     */
    @RequestMapping("/findAllItemRecords")
    public AjaxResult selectAllItemRecordsInfo(HttpServletRequest request) {
        // 1、获取路径参数
        String queryString = request.getQueryString();
        String staffID = getUrl(queryString, "StaffID");

        if (StringUtils.isBlank(staffID)) {
            return AjaxResult.error("未获取到制单人编号，请检查参数!!!!!");
        }

        // 2.根据staffId获取staff信息，将需要的参数传递给查询方法
        dGoodsCollectionItemRole role = new dGoodsCollectionItemRole();
        List<dGoodsCollectionItemRole> roles = dGoodsCollectionRoleService.selectPerInfoByStaffId(staffID);

        if (ObjectUtils.isEmpty(roles)) {
            return AjaxResult.error("未查询到商品列表信息!!!!!!");
        }
        {
            for (int i = 0; i < roles.size(); i++) {
                if (!"Q".equals(roles.get(i).getRoleId())) {
                    role.setRoleId(roles.get(i).getRoleId());
                    role.setStaffId(roles.get(i).getStaffId());
                    role.setUpstaffId(roles.get(i).getUpstaffId());
                    role.setSplx(roles.get(i).getSplx());
                    break;
                }
            }
        }
        // 3. 根据传递过来的createStaff
        if ("D".equals(role.getRoleId())) {
            return AjaxResult.success(dArcItemPreService.getAllItemDetailInfoByRoleId(staffID, role.getRoleId(), role.getUpstaffId()));
        }
        return AjaxResult.success(dArcItemPreService.getAllItemDetailInfo(staffID, role.getRoleId(), role.getUpstaffId()));
    }


    /**
     * 通过商品编码获取商品审批单数据
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/findItemInfoByItemId/{itemId}")
    public AjaxResult findItemInfoByItemId(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return AjaxResult.error(HttpStatus.ERROR, "该操作有误!!!!!");
        }
        dArcItemPre itemInfoByItem = null;
        try {
            itemInfoByItem = dArcItemPreService.getItemInfoByItemId(itemId);
            if (itemInfoByItem == null) {
                return AjaxResult.error(HttpStatus.ERROR, "未获取到该对象!!!!!");
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return AjaxResult.success(dArcItemPreService.getItemInfoByItemId(itemId));
    }


    /**
     * 根据商品编号修改商品状态信息
     * 初审操作
     *
     * @param itemId
     * @param status
     * @return
     */
    @PutMapping("/updateStaById/{itemId}/{status}")
    public AjaxResult updateItemStatusByItemId(@PathVariable String itemId, @PathVariable String status) {
        // 判断前端传递过来的参数是否为空
        if (StringUtils.isBlank(itemId) || StringUtils.isBlank(status)) {
            return AjaxResult.error("请检查你的操作是否正确!!!!");
        }

        if (StringUtils.isBlank(itemId) || StringUtils.isBlank(status)) {
            return AjaxResult.error("请检查你的操作是否正确!!!!");
        }
        String errorMessage = null;
        if (status.equals("000")) {
            int updateStatus = dArcItemPreService.updateItemInfoStatusByItemId(itemId, "001");
            if (updateStatus > 0) {
                errorMessage = "你已经完成这个【" + itemId + "】商品的初审操作!!!!!";
            }
        } else {
            int updateStatus = dArcItemPreService.updateItemInfoStatusByItemId(itemId, "000");
            if (updateStatus > 0) {
                errorMessage = "你已经撤销对这个【" + itemId + "】商品的初审操作， 将会回退到初始状态!!!!!";
            }
        }
        return AjaxResult.success(errorMessage);
    }


    /**
     * 根据商品编号修改商品状态信息
     * 终审操作
     *
     * @param itemId
     * @param status
     * @return
     */
    @PutMapping("/updateFinStaById/{itemId}/{status}")
    public AjaxResult updateItemFinalStatusByItemId(@PathVariable String itemId, @PathVariable String status) {
        // 判断前端传递过来的参数是否为空
        if (StringUtils.isBlank(itemId) || StringUtils.isBlank(status)) {
            return AjaxResult.error("请检查你的操作是否正确!!!!");
        }
        String errorMessage = null;
        if (status.equals("001")) {
            int updateStatus = dArcItemPreService.updateItemInfoStatusByItemId(itemId, "002");
            if (updateStatus > 0) {
                errorMessage = "你已经完成这个【" + itemId + "】商品的终审操作!!!!!";
            }
        } else {
            int updateStatus = dArcItemPreService.updateItemInfoStatusByItemId(itemId, "001");
            if (updateStatus > 0) {
                errorMessage = "你已经撤销对这个【" + itemId + "】商品的终审操作， 将会回退到初审状态!!!!!";
            }
        }
        return AjaxResult.success(errorMessage);
    }

    /**
     * 根据商品编号获取商品状态
     *
     * @param itemId
     * @return
     */
    @GetMapping("/getItemStatus/{itemId}")
    public AjaxResult getItemStatusByItemId(@PathVariable String itemId) {
        // 判断前端传递过来的参数是否为空
        if (StringUtils.isBlank(itemId)) {
            return AjaxResult.error("请检查你的操作是否正确!!!!");
        }

        return AjaxResult.success(dArcItemPreService.getItemStatusByItemId(itemId));
    }
}
